package com.shaonianyou.controller;

import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.shaonianyou.util.SignUtil;

@Controller
@RequestMapping("/auth")
public class AuthenticationController extends BaseController {

	@RequestMapping(value = "/sendCode/{idCard}/{mobile}", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getOpenId(@PathVariable String idCard, @PathVariable String mobile) {
		Map<String, Object> param = Maps.newHashMap();
		param.put("idcard", idCard);
		param.put("mobile", mobile);
		return doPost("/sendCode", param);
	}

	/**
	 * 微信开发者认证
	 * 
	 * @param response
	 * @param signature
	 * @param timestamp
	 * @param nonce
	 * @param echostr
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/wechatAuth", method = RequestMethod.GET)
	public void wechatAuth(HttpServletResponse response, String signature, String timestamp, String nonce,
			String echostr) throws Exception {

		PrintWriter out = response.getWriter();

		// 通过检验signature对请求进行校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败
		if (SignUtil.checkSignature(signature, timestamp, nonce)) {
			out.print(echostr);
		}

		out.close();
		out = null;

	}

	/**
	 * msgManager
	 * 
	 * @param response
	 * @param signature
	 * @param timestamp
	 * @param nonce
	 * @param echostr
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/wechatAuth", method = RequestMethod.POST)
	public String wechatAuthMsg(HttpServletResponse response, String signature, String timestamp, String nonce,
			String echostr) throws Exception {
		return null;
	}

}
